﻿using System.IO;
using System.Xml.Serialization;

namespace Codingly.SvnExternalUpdater.Services
{
    public class UserConfigurationService : IUserConfigurationService
    {
        private readonly XmlSerializer _serializer = new XmlSerializer(typeof(UserConfiguration));
        protected string FileName { get; set; }

        public UserConfigurationService()
        {
            FileName = "userSettings.xml";
        }

        public UserConfiguration GetConfiguration()
        {
            if (!File.Exists(FileName))
                return UserConfiguration.Default;

            using (var fileStream = new FileStream(FileName, FileMode.Open))
            {
                return _serializer.Deserialize(fileStream) as UserConfiguration;
            }
        }

        public void Save(UserConfiguration configuration)
        {
            using (var fileStream = new FileStream(FileName, FileMode.Create))
            {
                _serializer.Serialize(fileStream, configuration);
            }
        }
    }
}